/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.modules.biz.domain.VolunteerProfession;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.biz.service.VolunteerProfessionService;
import me.zhengjie.modules.biz.domain.vo.VolunteerProfessionQueryCriteria;
import me.zhengjie.modules.biz.mapper.VolunteerProfessionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author yiwei.he
* @date 2024-03-14
**/
@Service
@RequiredArgsConstructor
public class VolunteerProfessionServiceImpl extends ServiceImpl<VolunteerProfessionMapper, VolunteerProfession> implements VolunteerProfessionService {

    private final VolunteerProfessionMapper volunteerProfessionMapper;

    @Override
    public PageResult<VolunteerProfession> queryAll(VolunteerProfessionQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(volunteerProfessionMapper.findAll(criteria, page));
    }

    @Override
    public List<VolunteerProfession> queryAll(VolunteerProfessionQueryCriteria criteria){
        return volunteerProfessionMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(VolunteerProfession resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(VolunteerProfession resources) {
        VolunteerProfession volunteerProfession = getById(resources.getId());
        volunteerProfession.copy(resources);
        saveOrUpdate(volunteerProfession);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<VolunteerProfession> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (VolunteerProfession volunteerProfession : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("志愿表id", volunteerProfession.getVolunteerId());
            map.put("用户id", volunteerProfession.getUserId());
            map.put("专业id", volunteerProfession.getProfessionId());
            map.put("类型【1：高考志愿 2：单招 3.专升本】", volunteerProfession.getCategory());
            map.put("志愿中的专业排序", volunteerProfession.getProfessionSort());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void deleteProfessions(Long volunteerId,Integer category) {
        QueryWrapper<VolunteerProfession> wrapper = new QueryWrapper<>();
        wrapper.eq("volunteer_id", volunteerId).eq("category", category);
        volunteerProfessionMapper.delete(wrapper);
    }
}